package gitee.android.KUI.widget.Loading

import android.app.Activity
import android.content.Context
import android.graphics.Camera
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Matrix
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.animation.Animation
import android.view.animation.ScaleAnimation


/**
 * @author 程序员Keven
 * 时间 : 2023-01-09
 * 描述 : 抖音加载
 */
class DouYinLoading(
    context: Context?,
    attrs: AttributeSet? ,
) : View(context, attrs) {


    private var DouYinLoadingWith = 200;
    private var DouYinLoadingHeight = 120;

    private var r = 10f;

    private var activity : Activity = context as Activity;
    private var ballDis = 5f;
    private lateinit var leftPaint: Paint;
    private lateinit var rightPaint: Paint;

    private var  deg = 0f;


    init {
        initPaint()
    }

    private fun initPaint() {
        leftPaint = Paint();
        leftPaint.color = Color.parseColor("#28f2ef");
        leftPaint.strokeWidth = 1f;
        leftPaint.isAntiAlias = true;


        rightPaint = Paint();
        rightPaint.color = Color.parseColor("#fb2c58");
        rightPaint.strokeWidth = 1f;
        rightPaint.isAntiAlias = true;


    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {


        setMeasuredDimension(DouYinLoadingWith, DouYinLoadingHeight);
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        var matrix : Matrix;
        matrix = Matrix();
        var camera : Camera;
        camera = Camera();

        camera.rotateY(deg);
        camera.getMatrix(matrix)
        matrix.preTranslate(-(measuredWidth / 2f), -measuredHeight / 2f)
        matrix.postTranslate((measuredWidth / 2f), measuredHeight / 2f)
        canvas.setMatrix(matrix);


        canvas.drawCircle((measuredWidth / 2f)-r-ballDis , measuredHeight / 2f , r , leftPaint)
        canvas.drawCircle((measuredWidth / 2f)+r+ballDis , measuredHeight / 2f , r , rightPaint)



    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        makeRoate();
    }

    fun makeRoate(){

        postOnAnimation {
            deg+=10;
            activity.runOnUiThread {
                invalidate()
                requestLayout();
            }
        }

    }
}